/**
 * Created by Administrator on 2017/5/5 0005.
 */
require(["jquery","template","dm_cpt/doctorIndex","dm_store/doctorIndex","echarts"],function($,template,$cpt,$store,echarts) {
	
	/**
	 * 日期格式化
	 */
	var getDate = function(date){
        if(date.length==8){
            var year=date.substring(0,4);
            var month=date.substring(4,6);
            var day=date.substring(6,8);
            date=year+"-"+month+"-"+day;
        }else{
            year=date.substring(0,4);
            month=date.substring(4,6);
            date=year+"-"+month;
        }
        return date;
    }
	
	/**
	 * 科室患者基础信息统计渲染
	 */
	var patientReportNumber = function(){
		$store.patientReport(function (data) {
			if(!data)return;
	        var lis=$("#patientInfo").find("li");
	        lis.eq(0).find("p").html(data.data.patientTotal);
	        lis.eq(1).find("p").html(data.data.activeWeek);
	        lis.eq(2).find("p").html(data.data.activeWeekService);
	        lis.eq(3).find("p").html(data.data.patientAddWeek);
	        $("#allPatients").click(function(){
	            bb.openPage("allPatients");
	        })
	    });
	}
	
	/**
	 * 患者活跃趋势渲染
	 */
	var patientReportLine = function(){
		var myChart = echarts.init(document.getElementById("user-report-line-chart"));
		var option = $cpt.patientLine();
		myChart.setOption(option);
		var initLine = function(slot){
			$store.patientLine(slot,function(data){
				if(!data)return;
				var x=[];
	            var y=[];
	            for(var i=0;i<data.data.length;i++){
	                var date=data.data[i].date;
	                var dateX=getDate(date);
	                x.push(dateX);
	                y.push(data.data[i].count);
	            }
	            option.xAxis.data=x;
	            option.series[0].data=y;
	            myChart.setOption(option);
			});
		}
		
		//切换统计时段
		var onSwitchSlot = function(){
			var slot = $(this).attr("data-slot");
			initLine(slot);
		}
		$(".user-slot").click(onSwitchSlot);
		
		initLine(2);
	}
	
	/**
	 * 渲染新患者列表
	 */
	var newPatientList = function(){
		$store.newPatient(function(data){
			if(!data)return;
			var row=data.data.rows;
	        var lis="";
	        // console.log(row);
	        for(var i=0;i<row.length;i++){
	            var creatTime=row[i].createTime;
	            var d = new Date();
	            d.setTime(creatTime);
	            var date=d.getMonth()+"月"+d.getDate()+"日";
	            var minutes=d.getMinutes();
	            (minutes>=10) ? minutes:minutes="0"+minutes;
	            var time=d.getHours()+":"+minutes;
	            lis+='<li class="clear"><a href="#">\
	                    <div class="left listLeft"><i class="fa fa-user c-red fs-20" aria-hidden="true"></i></div>\
	                    <div class="left listRight">\
	                    <p><span>'+row[i].patientName+'</span><span>('+row[i].phoneNum+')</span></p>\
	                    <p class="fs-12"><span class="date right">'+date+'&nbsp;&nbsp;'+time+'</span></p>\
	                    </div>\
	            </a></li>';
	        }
	        $("#newPatient").append(lis);
		});
	}
	
	/**
	 * 科室服务信息统计渲染
	 */
	var serviceReportNumber = function(){
		$store.serviceReport(function(date){
			if(!data)return;
			for(var i=0;i<data.data.length;i++){
                if(data.data[i].serviceType=="1"){
                    $(".onOpen1").html(data.data[i].openServiceNum);
                    $(".onUsing1").html(data.data[i].useServiceNum);
                }else{
                    $(".onOpen2").html(data.data[i].openServiceNum);
                    $(".onUsing2").html(data.data[i].useServiceNum);
                }
            }
		});
	}
	
	/**
	 * 服务趋势统计
	 */
	var serviceReportLine = function(){
		var myChartA = echarts.init(document.getElementById(""));
		var myChartB = echarts.init(document.getElementById(""));
		var option = $cpt.seviceLine();
        myChartA.setOption(option);
        myChartB.setOption(option);
        
        var initLine = function(type,slot){
			$store.patientLine(type,slot,function(data){
				if(!data)return;
				var x=[];
                var y=[];
                for(var i=0;i<data.data.length;i++){
                    var date=data.data[i].date;
                    var dateX=getDate(date);
                    x.push(dateX);
                    y.push(data.data[i].count);
                }
                option.xAxis.data=x;
                option.series[0].data=y;
	            (type==1?myChartA:myChartB).setOption(option);
			});
		}
		
		//切换统计时段
		var onSwitchSlot = function(){
			var type = $(this).attr("data-type");
			var slot = $(this).attr("data-slot");
			initLine(type,slot);
		}
		$(".service-slot").click(onSwitchSlot);
		
		initLine(1,2);
		initLine(2,2);
	}
	

	/**
	 * 服务动态
	 */
	var serviceList = function(){
		$store.serviceList(function(data){
			if(!data)return;
			var lis="";
            for(var i=0;i<data.rows.length;i++){
                lis+='<li class="clear"><a href="#">\
                    <div class="left listVipLeft">\
                        <i class="fa fa-clock-o c-yellow fs-30" aria-hidden="true"></i>\
                        <p class="c-yellow fs-12">'+data.rows[i].opTimeDesc+'</p>\
                    </div>\
                    <div class="left listVipRight">\
                        <p>'+data.rows[i].opDesc+'</p>\
                    </div>\
                    </a></li>'
            }
            $("#officeActive").append(lis);
		});
	}

    
	/**
	 * 宣教阅读量统计渲染
	 */
	var missionReportLine = function(){
		var myChart = echarts.init(document.getElementById(""));
		var option = $cpt.missionLine();
        myChart.setOption(option);
        
        var initLine = function(slot){
			$store.missionLine(slot,function(data){
				if(!data)return;
				var x=[];
	            var y=[];
	            for(var i=0;i<data.data.length;i++){
	                var date=data.data[i].date;
	                var dateX=getDate(date);
	                x.push(dateX);
	                y.push(data.data[i].count);
	            }
	            option.xAxis.data=x;
	            option.series[0].data=y;
	            myChart.setOption(option);
			});
        }

		var onSwitchSlot = function(){
			var slot = $(this).attr("data-slot");
			initLine(type,slot);
		}
		$(".mission-slot").click(onSwitchSlot);
		
		initLine(2);
	}
	
	/**
	 * 宣教排行榜
	 */
	var missionList = function(){
		$store.missionList(function(data){
			if(!data)return;
			var lis="";
            for(var i=0;i<data.rows.length;i++){
                lis+='<li class="clear"><a href="#">\
                    <div class="left listLeft"><span class="cir"></span></div>\
                    <div class="left listRight">\
                    <p>'+data.rows[i].title+'</p>\
                    <p class="right fs-12"><i class="fa fa-eye c-yellow" aria-hidden="true"></i>&nbsp;&nbsp;<span class="num">'+data.rows[i].browseCount+'</span></p>\
                    </div>\
                    </a></li>';
            }
            $("#eduList").append(lis);
		});
	}

	/**
	 * 异常监测数据统计
	 */
	var monitorReportPie = function(){
		var myChartA = echarts.init(document.getElementById(""));
		var myChartB = echarts.init(document.getElementById(""));
		var option = $cpt.monitorPie();
        myChartA.setOption(option);
        myChartB.setOption(option);
        
        var initPie = function(type){
			$store.patientPie(type,function(data){
				if(!data)return;
				var abnormal=data.data[0].abnormalCount;
                var total=data.data[0].monitorCount;
                var normal=total-abnormal;
                var rate=parseInt(100*abnormal/total);//异常占比
                $(".total"+type).html(total);
                $(".proportion"+type).html(rate+"%");
                option.series=[{
                    name:'访问来源',
                    type:'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    label: {
                        normal: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:[
                        {value:abnormal, name:'异常'},
                        {value:normal, name:'正常'}
                    ]
                }];
                myChart.setOption(option);
	            (type==1?myChartA:myChartB).setOption(option);
			});
		}
		
		//切换统计时段
		var onSwitchSlot = function(){
			var type = $(this).attr("data-type");
			initLine(type,slot);
		}
		$(".monitor-slot").click(onSwitchSlot);
		
		initPie(1);
		initPie(3);
	}
	
	/**
	 * 氧饱脉率监测量趋势
	 */
	var monitorReportLine = function(){
		var myChart = echarts.init(document.getElementById(""));
		var config = config.monitorLine();
		myChart.setOption(option);
        
        var initLine = function(slot){
			$store.monitorLine(slot,function(data){
				if(!data)return;
				var yTem=[];
                for(var i=0;i<data.data.length;i++){
                    yTem.push(data.data[i].count);
                }
                console.log("氧饱脉率:"+yTem);
                var obj={
                        name:'氧饱脉率',
                        type:'line',
                        data:yTem
                    };
                option.series.push(obj);
                console.log(option.series);
                myChart.setOption(option);
			});
        }

		var onSwitchSlot = function(){
			var slot = $(this).attr("data-slot");
			initLine(type,slot);
		}
		$(".mission-slot").click(onSwitchSlot);
		
		initLine(2);
	}
	
	/**
	 * 监测记录
	 */
	var monitorList = function(){
		$store.monitorList(function(data){
			if(!data)return;
			var lis="";
            for(var i=0;i<data.rows.length;i++){
                    lis+='<li class="clear"><a href="#">\
                    <div class="left listLeft"><span class="cir"></span></div>\
                    <div class="left listRight">\
                    <p class="fs-14">'+data.rows[i].opDesc+'<span class="right fs-12">'+data.rows[i].opTimeDesc+'</span></p>\
                </div>\
                </a></li>'
            }
            $("#patientWatchInfo").append(lis);
		});
	};
	
	/**
	 * 渲染患者统计模块
	 */
	(function(){
		patientReportNumber();
		patientReportLine();
		newPatientList();
	})();
	
	/**
	 * 延迟渲染服务统计
	 */
/*	setTimeout(() => {
		serviceReportNumber();
		serviceReportLine();
		serviceList();
	}, 1000);
	
	/!**
	 * 延迟渲染宣教统计
	 *!/
	setTimeout(() => {
		missionReportLine();
		missionList();
	}, 2000);
	
	/!**
	 * 延迟渲染监测统计
	 *!/
	setTimeout(() => {
		monitorReportPie();
		monitorReportLine();
		monitorList()
	}, 3000);*/
});